<template>
  <div class="app-container flex-col minH">
    <div class="topline w100p bgwhite pt20 pb10 radius10 pl10 pr20 justify-between">
      <div>
        <el-input v-model="id" style="width: 280px" placeholder="搜尋ID..." :prefix-icon="Search" class="search mr10 mb10" />
        <el-input v-model="name" style="width: 280px" placeholder="搜尋分類名稱..." :prefix-icon="Search" class="search mr10 mb10" />

      </div>
      <div class="noshrink mb10">
        <el-button class="btn2" @click="search()">查詢</el-button>
        <el-button class="btn1" @click="reset()">重置</el-button>
      </div>
    </div>
    <div class="mt15 radius10 hidden pb15 flex1 bgwhite  flex-col w100p">
      <div class="flex1 w100p ">
        <el-table :data="tableData" style="width: 100%" cell-class-name="tbcell" @selection-change="handleSelectionChange" header-cell-class-name="hdcell">
          <el-table-column type="selection" width="55" />
          <el-table-column prop="id" label="序號" width="100" sortable />

          <el-table-column prop="name" label="分類名稱" width="120" />
          <el-table-column prop="min_price" label="最低價格(/小時)" width="150" />
          <el-table-column prop="max_price" label="最高價格(/小時)" width="150" />
          <el-table-column prop="logo" label="分類Logo" width="200">
            <template #default="scope">
              <el-avatar shape="square" :size="50" :fit="'cover'" :src="scope.row.logo" />
            </template>
          </el-table-column>
          <el-table-column prop="created_at" label="創建時間" min-width="200" />
          <el-table-column prop="updated_at" label="更新時間" min-width="200" sortable />
          <el-table-column prop="" label="操作" width="200" fixed="right">
            <template #default="scope">
              <div class="align-center">
                <div class=" pointer" @click="edit(scope.row, 'edit')">編輯</div>
                <span class="plr10">|</span>
                <div class=" pointer red" @click="toDel(scope.row)">刪除</div>
              </div>
            </template>
          </el-table-column>

        </el-table>
      </div>
      <div class=" justify-between plr20 w100p mt15">
        <div class="align-center">
          <el-button class="btn1" @click="batchDel()">刪除</el-button>

          <el-button class="btn2" @click="toAdd()">新增</el-button>
        </div>
        <el-pagination v-model:current-page="currentPage" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>

    </div>
  </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router';
import { classifyList, deleteClassify } from '@/api/player.js'
onMounted(() => {
  getClassifyList()
})
// 分类列表
const getClassifyList = () => {
  classifyList({
    page: currentPage.value,
    id: id.value,
    name: name.value
  }).then((res) => {
    console.log('分类列表', res)
    tableData.value = res.data.data
    total.value = res.data.total
  })
}
const name = ref('')
const id = ref('')
const dialogTableVisible = ref(false)
const dialogType = ref(1)
const dialogShow = (type) => {
  console.log('dialog----show----')
  dialogType.value = type
  dialogTableVisible.value = true
}
// 重置
const reset = () => {
  id.value = ''
  name.value = ''
  getClassifyList()

}
// 搜尋
const search = () => {
  getClassifyList()
}
const tableData = ref([])
const tableRowClassName = () => {
  return 'tbrow'
}
const currentPage = ref(1)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  getClassifyList()
}
const idStr = ref('')
const handleSelectionChange = (e) => {
  let idList = e.map((item, index) => { return item.id })
  idStr.value = idList.join(',')
  console.log(idList)
}
const batchDel = () => {
  ElMessageBox.confirm(
    '确认要删除吗?',
    'Warning',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
      title: '删除'
    }
  )
    .then(() => {
      deleteClassify({
        id: idStr.value
      }).then((res) => {
        console.log(res)
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        getClassifyList()
      })

    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })

}
// 删除
const toDel = (row) => {
  ElMessageBox.confirm(
    '确认要删除吗?',
    'Warning',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
      title: '删除'
    }
  )
    .then(() => {
      deleteClassify({
        id: row.id
      }).then((res) => {
        console.log(res)
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        getClassifyList()
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}


const router = useRouter()
const toAdd = () => {
  console.log(router)
  router.push({ path: 'addClassify' })
}
const edit = (row, type) => {
  router.push({ path: 'editClassify', query: { ...row, type: type } })
}
</script>

<style lang="scss" scoped>
.search {
  overflow: hidden;
  border-radius: 10px !important;
  // padding: 5px 0px;
}

:deep(.el-input__wrapper) {
  box-shadow: none;
}

:deep(.el-button--primary) {
  --el-button-bg-color: #3a3f63 !important;
}
</style>
<style></style>
 